package com.workpaper.app.workpaper.daily.business;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.workpaper.app.db.mybatis.MyDB;
import com.workpaper.app.workpaper.daily.entity.DailyWorkpaper;
import com.workpaper.app.workpaper.daily.mapper.DailyWorkpaperMapper;

/**
 * 工作日报业务类
 * @author zyj
 *
 */
public class DailyWorkpaperBusiness {
	private static class Holder{
		private static DailyWorkpaperBusiness INSTANCE = new DailyWorkpaperBusiness();
	}

	private DailyWorkpaperBusiness() {
		super();
	}
	
	public static DailyWorkpaperBusiness getInstance() {
		return Holder.INSTANCE;
	}
	
	/**
	 * 新增工作日志
	 * @param workpaperDaily
	 */
	public void addOneWorkpaperDaily(DailyWorkpaper workpaperDaily) {
		if(workpaperDaily == null || StringUtils.isNotBlank(workpaperDaily.getId())) {
			//为空，或者id已存在，表示要进行更新操作
			return;
		}
		try(SqlSession sqlSession = MyDB.openSession()){
			DailyWorkpaperMapper mapper = sqlSession.getMapper(DailyWorkpaperMapper.class);
			mapper.saveWorkpaperDaily(workpaperDaily);
			sqlSession.commit();
		}
	}
	
	/**
	 * 查询所有工作日报
	 * @return
	 */
	public List<DailyWorkpaper> getAllWorkpaperDaily(){
		try(SqlSession sqlSession = MyDB.openSession()){
			DailyWorkpaperMapper mapper = sqlSession.getMapper(DailyWorkpaperMapper.class);
			return mapper.getAllWorkpaperDaily();
		}
	}

	/**
	 * 根据主键查询
	 * @return
	 */
	public DailyWorkpaper selectById(String id){
		try(SqlSession sqlSession = MyDB.openSession()){
			DailyWorkpaperMapper mapper = sqlSession.getMapper(DailyWorkpaperMapper.class);
			return mapper.selectById(id);
		}
	}
	/**
	 * 删除工作日报
	 * @param id
	 */
	public boolean deleteById(String id) {
		try(SqlSession sqlSession = MyDB.openSession()){
			DailyWorkpaperMapper mapper = sqlSession.getMapper(DailyWorkpaperMapper.class);
			int deledNum = mapper.deleteById(id);
			sqlSession.commit();
			return deledNum == 1;
		}
	}
	
	/**
	 * 更新一条工作日报
	 * @param workpaperDaily
	 */
	public void updateOne(DailyWorkpaper workpaperDaily) {
		if(workpaperDaily == null || StringUtils.isBlank(workpaperDaily.getId())) {
			//为空，或者id已存在，表示要进行更新操作
			return;
		}
		try(SqlSession sqlSession = MyDB.openSession()){
			DailyWorkpaperMapper mapper = sqlSession.getMapper(DailyWorkpaperMapper.class);
			mapper.updateOne(workpaperDaily);
			sqlSession.commit();
		}
	}
}
